﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.IO;

using System.Diagnostics;

namespace WindowsApplication2
{
    public partial class WiZiBar : Form
    {
        public WiZiBar()
        {
            InitializeComponent();
        }

        private int bx = 0;
        private int by = 0;
        private int lx = 200;
        private int ly = 100;
        private int barData = 1;
        private string barFont = "";
        private string barBack = "";
        private int barLie = 1;
        private int barNum = 1;

        private void comType_SelectedIndexChanged(object sender, EventArgs e)
        {
            barcodeControl2.BarcodeType = GetBarcodeType(comType.Text);
            Invalidate();
        }

        BarcodeType GetBarcodeType(string cType)
        {
            BarcodeType bt = BarcodeType.CODE128A;
            switch (cType.ToUpper())
            {
                case "CODE128A":
                    bt = BarcodeType.CODE128A;
                    break;
                case "C2OF5":
                    bt = BarcodeType.C2OF5;
                    break;
                case "CODABAR":
                    bt = BarcodeType.CODABAR;
                    break;
                case "CODE128B":
                    bt = BarcodeType.CODE128B;
                    break;
                case "CODE128C":
                    bt = BarcodeType.CODE128C;
                    break;
                case "CODE39":
                    bt = BarcodeType.CODE39;
                    break;
                case "CODE39CHECK":
                    bt = BarcodeType.CODE39CHECK;
                    break;
                case "CODE93":
                    bt = BarcodeType.CODE93;
                    break;
                case "EAN128A":
                    bt = BarcodeType.EAN128A;
                    break;
                case "EAN128B":
                    bt = BarcodeType.EAN128B;
                    break;
                case "EAN128C":
                    bt = BarcodeType.EAN128C;
                    break;
                case "EAN13":
                    bt = BarcodeType.EAN13;
                    break;
                case "EAN13_2":
                    bt = BarcodeType.EAN13_2;
                    break;
                case "EAN13_5":
                    bt = BarcodeType.EAN13_5;
                    break;
                case "EAN8":
                    bt = BarcodeType.EAN8;
                    break;
                case "EAN8_2":
                    bt = BarcodeType.EAN8_2;
                    break;
                case "EAN8_5":
                    bt = BarcodeType.EAN8_5;
                    break;
                case "INTERLEAVED2OF5":
                    bt = BarcodeType.INTERLEAVED2OF5;
                    break;
                case "MSIPLESSEY":
                    bt = BarcodeType.MSIPLESSEY;
                    break;
                case "MSIPLESSEYCHECK10":
                    bt = BarcodeType.MSIPLESSEYCHECK10;
                    break;
                case "MSIPLESSEYCHECK1010":
                    bt = BarcodeType.MSIPLESSEYCHECK1010;
                    break;
                case "MSIPLESSEYCHECK11":
                    bt = BarcodeType.MSIPLESSEYCHECK11;
                    break;
                case "MSIPLESSEYCHECK1110":
                    bt = BarcodeType.MSIPLESSEYCHECK1110;
                    break;
                case "PLANET":
                    bt = BarcodeType.PLANET;
                    break;
                case "POSTNET":
                    bt = BarcodeType.POSTNET;
                    break;
                case "ROYALMAIL":
                    bt = BarcodeType.ROYALMAIL;
                    break;
                case "UPCA":
                    bt = BarcodeType.UPCA;
                    break;
                case "UPCA_2":
                    bt = BarcodeType.UPCA_2;
                    break;
                case "UPCA_5":
                    bt = BarcodeType.UPCA_5;
                    break;
                case "UPCE":
                    bt = BarcodeType.UPCE;
                    break;
                case "UPCE_2":
                    bt = BarcodeType.UPCE_2;
                    break;
                case "UPCE_5":
                    bt = BarcodeType.UPCE_5;
                    break;
            }
            return bt;
        }

        private void cheCode39_CheckedChanged(object sender, EventArgs e)
        {
            barcodeControl2.ShowCode39StartStop = cheCode39.Checked;
            Invalidate();
        }

        private void txtData_TextChanged(object sender, EventArgs e)
        {
           // barcodeControl2.Data = txtData.Text;
            //Invalidate();
        }

        private void txtSubData_TextChanged(object sender, EventArgs e)
        {
            barcodeControl2.AddOnData = txtSubData.Text;
            Invalidate();
        }

        private void comDataShow_SelectedIndexChanged(object sender, EventArgs e)
        {
            barcodeControl2.TextPosition = GetTextPosition(comDataShow.Text);
            Invalidate();
        }

        BarcodeTextPosition GetTextPosition(string str)
        {
            switch (str)
            {
                case "顶部":
                    return BarcodeTextPosition.Above;
                case "不显示":
                    return BarcodeTextPosition.NotShown;
                case "底部":
                default:
                    return BarcodeTextPosition.Below;
            }
        }

        private void BarCodeSetupBox_Load(object sender, EventArgs e)
        {
            comType.SelectedIndex = 0;
            comDataShow.SelectedIndex = 0;
            Invalidate();
        }

        private void vistaButton1_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            PrintPreviewDialog cppd = new PrintPreviewDialog();
            cppd.Document = pd;
            cppd.ShowDialog();
        }

        private void GetFormData()
        {
            this.bx = (int)numericUpDown2.Value;
            this.by = (int)numericUpDown3.Value;
            if (this.txtLx.Text.Trim()!="")
                this.lx = Convert.ToInt32(this.txtLx.Text.Trim());
            if (this.txtly.Text.Trim() != "")
                this.ly = Convert.ToInt32(this.txtly.Text.Trim());
           
            if (this.txtMlie.Text.Trim() != "")
                this.barLie = Convert.ToInt32(this.txtMlie.Text.Trim());
          
        }

        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            GetFormData();
            Graphics g = e.Graphics;
            string bar = "";
            if (radb1.Checked == true)
            {
                int qu=1;
                int lie=1;
                int x=0;
                int y=0;
                if(this.txtQu1.Text.Trim()!="")
                    qu=Convert.ToInt32(this.txtQu1.Text.Trim());
                if(this.txtLie1.Text.Trim()!="")
                    lie=Convert.ToInt32(this.txtLie1.Text.Trim());

                for (int j = 1; j <= lie; j++)
                {
                    bar = "";
                    if (qu < 10)
                        bar += "0" + qu.ToString();
                    else
                        bar += qu.ToString();
                    if (j < 10)
                        bar += "0" + j.ToString();
                    else
                        bar += j.ToString();
                    bar += "000000";
                    Rectangle rect = barcodeControl2.ClientRectangle;
                    //下面是调整打印位置的代码。
                    if (j-1 < this.barLie)
                    {
                        if ((j-1) == 0)
                        {
                            x = bx;
                        }
                        else
                        {
                            x = x + bx + lx;
                        }
                    }
                    else
                    {
                        if ((j-1) % this.barLie == 0)
                        {
                            x = bx;
                            y = y + by + ly;

                        }
                        else
                        {
                            x = x + bx + lx;
                        }
                    }
                    barcodeControl2.CopyRight = qu.ToString() + "区" + j.ToString() + "列";
                    barcodeControl2.Data = bar;
                    rect = new Rectangle(rect.X + x, rect.Y + y, rect.Width, rect.Height);
                    //rect1 = new Rectangle(rect.X + 500, rect.Y + 500, rect.Width, rect.Height);
                    //打印
                    barcodeControl2.Draw(g, rect, GraphicsUnit.Inch, 0.01f, 0, null);
                }
            }
            else if (radb2.Checked == true)
            {
                int qu = 1;
                int lie = 1;
                int cen = 1;
                int jie = 1;
                int x = 0;
                int y = 0;
                if (this.txtQu2.Text.Trim() != "")
                    qu = Convert.ToInt32(this.txtQu2.Text.Trim());
                if (this.txtLie2.Text.Trim() != "")
                    lie = Convert.ToInt32(this.txtLie2.Text.Trim());
                if (this.txtCen2.Text.Trim() != "")
                    cen = Convert.ToInt32(this.txtCen2.Text.Trim());
                if (this.txtJie2.Text.Trim() != "")
                    jie = Convert.ToInt32(this.txtJie2.Text.Trim());

                for (int j = 0; j <= 1; j++)
                {
                    bar = "";
                    if (qu < 10)
                        bar += "0" + qu.ToString();
                    else
                        bar += qu.ToString();
                    if (lie < 10)
                        bar += "0" + lie.ToString();
                    else
                        bar += lie.ToString();
                    if (jie < 10)
                        bar += "0" + jie.ToString();
                    else
                        bar += jie.ToString();
                    if (cen < 10)
                        bar += "0" + cen.ToString();
                    else
                        bar += cen.ToString();

                    if (j == 0)
                        bar += "01";
                    else
                        bar += "02";
                    Rectangle rect = barcodeControl2.ClientRectangle;
                    //下面是调整打印位置的代码。
                    if ((j - 1) == 0)
                    {
                        x = bx;
                    }
                    else
                    {
                        x = x + bx + lx;
                    }
                    
                    barcodeControl2.CopyRight = qu.ToString() + "区" + lie.ToString() + "列"+jie.ToString()+"节"+cen.ToString()+"层";
                    if (j == 0)
                        barcodeControl2.CopyRight += "左侧";
                    else
                        barcodeControl2.CopyRight += "右侧";
                    barcodeControl2.Data = bar;
                    rect = new Rectangle(rect.X + x, rect.Y + y, rect.Width, rect.Height);
                    //rect1 = new Rectangle(rect.X + 500, rect.Y + 500, rect.Width, rect.Height);
                    //打印
                    barcodeControl2.Draw(g, rect, GraphicsUnit.Inch, 0.01f, 0, null);
                }
            }
            else if (radb3.Checked == true)
            {
                Rectangle rect = barcodeControl2.ClientRectangle;
                barcodeControl2.CopyRight =this.txtBname.Text.Trim();
                barcodeControl2.Data = this.txtBar.Text.Trim();
                rect = new Rectangle(rect.X + bx, rect.Y + by, rect.Width, rect.Height);
                //rect1 = new Rectangle(rect.X + 500, rect.Y + 500, rect.Width, rect.Height);
                //打印
                barcodeControl2.Draw(g, rect, GraphicsUnit.Inch, 0.01f, 0, null);
 
            }
            

           
            g.Dispose();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            barcodeControl2.StretchText = checkBox1.Checked;
        }

        void txtTitle_TextChanged(object sender, System.EventArgs e)
        {
            //barcodeControl2.CopyRight = txtTitle.Text;
        }

        private void vistaButton3_Click(object sender, EventArgs e)
        {
            System.IO.Stream myStream;
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Filter = "GIF 文件 (*.gif)|*.gif|PNG 文件 (*.png)|*.png|JPEG 文件 (*.jpg;*.jpeg)|*.jpg;*.jpeg|BMP 文件 (*.bmp;*.dib)|*.bmp;*.dib|TIFF 文件 (*.tif;*.tiff)|*.tif;*.tiff";
            fileDialog.FilterIndex = 1;
            fileDialog.FileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            fileDialog.RestoreDirectory = false;
            ImageFormat[] afmt = { ImageFormat.Gif, ImageFormat.Png, ImageFormat.Jpeg, ImageFormat.Bmp, ImageFormat.Tiff };

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = fileDialog.OpenFile()) != null)
                {
                    int barcodeWidth = 1;
                    int barcodeHeight = 50;
                    barcodeControl2.MakeImage(afmt[fileDialog.FilterIndex - 1], barcodeWidth, barcodeHeight, true, false, null, myStream);
                    myStream.Close();
                }
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
        }

        private void vistaButton2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                //Process.Start("http://www.cobainsoft.com/BarCodeControl/");
            }
            catch { }
        }

        private void txtLie_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}